<?php
class class_product extends abstract_product implements interface_product {
    public $error;

    // External Relations
    protected $category_id = null;
    protected $store_id = null;
    protected $manufacturer = '';
    protected $stock_status = '';
    protected $keyword = '';
    protected $special = null;
    protected $reward = 0;
    protected $reviews = 0;
    protected $rating = 0;
    protected $weight_class;
    protected $length_class;
    protected $discount;

    // product to description relation
    // use as array of description for
    // each language but store values
    // from fetchObjet 
    protected $dl = array();
    private $language_id;
    private $name;
    private $description;
    private $meta_description;
    private $meta_keyword;
    private $tag;

    public function getData(){ 
        return get_object_vars($this);
   }
 
    public function setDescription(interface_product_description $description){
        if($description instanceof interface_product_description){
            $this->dl[$description->getLanguageId()] = $description;
            return $this;
        }
        throw new Exception('Object description do not implement interface_ product_description');
    }

    public function getDescription($language_id){
        return (isset($this->dl[$language_id]) ? $this->dl[$language_id] : false);
    }
    
    public function getDescriptions(){
        return $this->dl;
    }

    public function setSpecial($price){
        $this->special = $price;
        return $this;
    }

    public function getSpecial(){
        return $this->special;
    }

    public function setReward($reward){
        $this->reward = $reward;
    }

    public function getReward(){
        return $this->reward;
    }

    public function setRating($rating){
        $this->rating = round($rating);
        return $this;
    }

    public function getRating(){
        return $this->rating;
    }

    public function setReviews($reviews){
        $this->reviews = $reviews;
        return $this;
    }

    public function getReviews(){
        return $this->reviews;
    }

    public function setStockStatus($stockStatus){
        $this->stock_status = $stockStatus;
    }

    public function getStockStatus(){
        return $this->stock_status;
    }

    public function setKeyword($keyword){
        $this->keyword = $keyword;
        return $this;
    }

    public function getKeyword(){
        return $this->keyword;
    }

    public function setCategoryId($categoryId){
        $this->category_id = $categoryId;
        return $this;
    }

    public function getCategoryId(){
        return $this->category_id;
    }

    public function setStoreId($storeId){
        $this->store_id = $storeId;
        return $this;
    }

    public function getStoreId(){
        return $this->store_id;
    }


    public function setManufacturer($manufacturer){
        $this->manufacturer = $manufacturer;
        return $this;
    }

    public function getManufacturer(){
        return $this->manufacturer;
    }

    public function setData($data){
        parent::setData($data);

        if(isset($data['category_id'])){
            $this->setCategoryId($data['categoryId']);
        }

        if(isset($data['store_id'])){
            $this->storeId = $data['store_id'];
        }

        if(isset($data['special'])){
           $this->setIpecial($data['special']);
        }

        if(isset($data['reward'])){
            $this->setReward($data['reward']);
        }

        if(isset($data['rating'])){
            $this->setRating($data['rating']);
        }
    
        if(isset($data['manufacturer'])){
            $this->setManufacturer($data['manufacturer']);
        }

        if(isset($data['keyword'])){
            $this->setKeyword($data['keyword']);
        }
 
        if(isset($data['reviews'])){
            $this->setReviews($data['reviews']);
        }

        if(isset($data['stock_status'])){
            $this->setStockStatus($data['stock_status']);
        }

    }

    public function validate(){
		
        $this->error = array();

        $model = $this->get_model();
    	if ((strlen($model) < 1) || (strlen($model) > 64)) {
      		$this->error[] = 'error_model';
    	}
	
        $descriptions = $this->get_descriptions();
    	foreach ($descriptions as $language_id => $description) {
      		if ((strlen($description->name) < 1) || (strlen($description->name) > 255)) {
        		$this->error[]['error_name'] = $language_id;
      		}
    	}
		
        if ((strlen($this->keyword) < 1) || (strlen($this->keyword) > 255)) {
            $this->error[] = 'error_keyword';
        }

        if(is_null($this->category_id())){
            $this->error[] = 'error_category_id';
        }
 
        if(is_null($this->store_id())){
            $this->error[] = 'error_store_id';
        }
 
        if(is_null($this->manufacturer_id())){
            $this->error[] = 'error_manufacturer_id';
        }

        if(is_null($this->stock_status_id())){
            $this->error[] = 'error_stock_status_id';
        }

        if(is_null($this->tax_class_id())){
            $this->error[] = 'error_tax_class_id';
        }
    
        if(is_null($this->weight_class_id())){
            $this->error[] = 'error_weight_class_id';
        }

        if(is_null($this->get_length_class_id())){
            $this->error[] = 'error_lenght_class_id';
        }

        if(is_null($this->get_price)){
            $this->error[] = 'error_price';
        }
		
    	if (empty($this->error)) {
			return true;
    	} 
      	return false;
    }
}
